
#include "tcx_parser.h"

#include <stdlib.h>
#include "glog/logging.h"
#include "gps.pb.h"
#include "date_time.h"

namespace gps_proto {

TcxParser::TcxParser(GpsProto* gps_proto)
  : GpsParser(gps_proto) {
}

// static
bool TcxParser::ParseFromFile(const char* filename, GpsProto* gps_proto) {
}

// static
bool TcxParser::ParseFromIstream(std::istream* input, GpsProto* gps_proto) {
}

// static
bool TcxParser::ParseFromString(const std::string& gps, GpsProto* gps_proto) {
}

// virtual
void TcxParser::StartElement(const char *name, const char **atts) {
  VLOG(2) << "StartElement " << name;
  if (strcmp("Lap", name) == 0) {
    if (!gps_proto_) {
      VLOG(1) << "ignoring <Lap>";
      return;
    }
    track_proto_ = gps_proto_->add_track();
  } else if (strcmp("Track", name) == 0) {
    if (!track_proto_) {
      VLOG(1) << "ignoring <Track> outside of <Lap>";
      return;
    }
    segment_proto_ = track_proto_->add_segment();
  } else if (strcmp("Trackpoint", name) == 0) {
    if (!segment_proto_) {
      VLOG(1) << "ignoring <Trackpoint> outside of <Track>";
      return;
    }
    point_proto_ = segment_proto_->add_point();
  } else if (strcmp("Time", name) == 0) {
    GatherCharData();
  } else if (strcmp("AltitudeMeters", name) == 0) {
    GatherCharData();
  } else if (strcmp("Position", name) == 0) {
  } else if (strcmp("LatitudeDegrees", name) == 0) {
    GatherCharData();
  } else if (strcmp("LongitudeDegrees", name) == 0) {
    GatherCharData();
  }
}

// virtual
void TcxParser::EndElement(const char *name) {
  if (strcmp("Lap", name) == 0) {
    track_proto_ = NULL;
  } else if (strcmp("Track", name) == 0) {
    segment_proto_ = NULL;
  } else if (strcmp("Trackpoint", name) == 0) {
    point_proto_ = NULL;
  } else if (strcmp("Time", name) == 0) {
    if (!point_proto_) {
      VLOG(1) << "ignoring <Time> outside of <Trackpoint>";
      return;
    }
    point_proto_->set_time(DateTimeToTimeT(GetCharData()));
  } else if (strcmp("AltitudeMeters", name) == 0) {
    if (!point_proto_) {
      VLOG(1) << "ignoring <AltitudeMeters> outside of <Trackpoint>";
      return;
    }
    point_proto_->set_ele(strtod(GetCharData().c_str(), NULL));
  } else if (strcmp("Position", name) == 0) {
  } else if (strcmp("LatitudeDegrees", name) == 0) {
    if (!point_proto_) {
      VLOG(1) << "ignoring <LatitudeDegrees> outside of <Trackpoint>";
      return;
    }
    point_proto_->set_lat(strtod(GetCharData().c_str(), NULL));
  } else if (strcmp("LongitudeDegrees", name) == 0) {
    if (!point_proto_) {
      VLOG(1) << "ignoring <LatitudeDegrees> outside of <Trackpoint>";
      return;
    }
    point_proto_->set_lon(strtod(GetCharData().c_str(), NULL));
  }
}

}
